import java.io.*;
import java.nio.*;
import java.net.*;

public class DummyShaper 
{
	public static final int MAX_PACKET_SIZE = 1490;

	public static void main(String[] args) throws IOException 
	{
		if(args.length != 2)
		{
			System.err.println("Usage: java DummyShaper trafficShaperPortNo receiverIP(or hostname) ");
			return;
		}
        DatagramSocket socket = new DatagramSocket(Integer.parseInt(args[0]));
        InetAddress address = InetAddress.getByName(args[1]);
        
        ByteBuffer buf = ByteBuffer.allocate(MAX_PACKET_SIZE);
        while (true) 
        {
            buf.rewind();
            DatagramPacket packet = new DatagramPacket(buf.array(), MAX_PACKET_SIZE);
            socket.receive(packet);
            long recvTime = System.nanoTime();
            buf.rewind();
            int seq = buf.getInt();
            int portNo=buf.getInt(); 
            DatagramPacket pack = new DatagramPacket(buf.array(), packet.getLength(), address, portNo);
            socket.send(pack);
            System.out.println("Sending packet to port "+portNo );
        }
	}

}
